using UnityEngine;
using System.Collections;

public class CoinsCreator : MonoBehaviour 
{
	public float intervalTime = 3;
	
	private float timer;
	
	private CoinsMapsManager coinMapsManager;
	
	// Leitor
	public LeitorDeCoins leitorDeCoins;
	
	public GameObject coinPrefab;
	
	public int startX = 10;
	public int startY = 8;
	
	public float xDistancePerEach;
	public float yDistancePerEach;
	
	private bool running;
	
	void Start () 
	{
		this.coinMapsManager = new CoinsMapsManager();
		this.leitorDeCoins.Setup();
		running = true;
		
	}
	
	void Update () 
	{
		if(running)
		{
			timer += Time.deltaTime;
			
			if(timer >= this.intervalTime)
			{
				//int[,] tmpCoinMap = coinMapsManager.getCoinMap(CoinsMapsManager.HEART_COIN_MAP);
				
				int[,] tmpCoinMap = leitorDeCoins.getRandomCoinMap();
				if(tmpCoinMap != null){
				
				for (int i = 0; i < tmpCoinMap.GetLength(0); i++) {
					for (int j = 0; j < tmpCoinMap.GetLength(1); j++) {
						
						if(tmpCoinMap[i, j] == CoinsMapsManager.ConditionChar)
						Instantiate(coinPrefab,new Vector3(this.startX + j * xDistancePerEach, this.startY + -i * yDistancePerEach, coinPrefab.transform.position.z),Quaternion.identity);	
					}
				}
				
				this.timer = 0;
					
				}
			}
		}
	}
	
	public void Stop()
	{
		running = false;
	}
}